package com.happyscan138.makefun.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.happyscan138.makefun.MainActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;

/**
 * Created by Administrator on 2017/1/20.
 */

public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    String APP_ID = "wxb3ba5a6e1c615e27";//改成你自己的APP_ID
    private IWXAPI api;
    private BaseResp resp = null;
    // 获取第一步的code后，请求以下链接获取access_token
    private String GetCodeRequest = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
    public  String key;
    public String unionid,nick;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, APP_ID);
        api.registerApp(APP_ID);
        api.handleIntent(getIntent(), this);
    }


    @Override

    public void onReq(BaseReq arg0) {
        System.out.println("onRep");
    }


    @Override

    public void onResp(final BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                SendAuth.Resp response = (SendAuth.Resp) resp;
                String code=response.code;
                Log.e("code",code);
                client=new AsyncHttpClient();
                params=new RequestParams();
                params.put("code",code);

                RequestHandle handle=client.post("http://120.25.149.212/wenda/cli-wx-login.php",params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();

                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            JSONObject obj=jsonObject.getJSONObject("data");
                           if(obj.length()==1){
                                key=obj.getString("access_key");

                           }else if(obj.length()==2){
                               unionid=obj.getString("unionid");
                               nick=obj.getString("nickname");
                               Intent intent=new Intent();
                               intent.putExtra("unionid",unionid);
                               intent.putExtra("nick",nick);
                               intent.setClass(WXEntryActivity.this,MainActivity.class);
                               startActivity(intent);

                           }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });


                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:

                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:

                break;
            default:
                break;

        }
            this.finish();
    }
    private String getCodeRequest(String code) {
        String result = null;
        GetCodeRequest = GetCodeRequest.replace("APPID",APP_ID);
        GetCodeRequest = GetCodeRequest.replace("SECRET","" );
        GetCodeRequest = GetCodeRequest.replace("CODE", code);
        result = GetCodeRequest;
        return result;
    }

}
